package cn.acac.yygh.cmn.listener;

import cn.acac.yygh.model.cmn.Dict;
import cn.acac.yygh.vo.cmn.DictEeVo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 导入Excel的监听器
 */
public class DictListener extends AnalysisEventListener<DictEeVo> {

    private BaseMapper<Dict> baseMapper;
    public DictListener(BaseMapper baseMapper){
        this.baseMapper = baseMapper;
    }

    /**
     * 读取一行excel的数据就会执行一次这个方法
     * @param dictEeVo
     * @param analysisContext
     */
    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        // 将dietEevo转换为Dict
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo,dict);

        // 判断导入的信息数据库是否存在
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dict.getId());
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count > 0){
            // 进行更新操作
            baseMapper.updateById(dict);
        }else {
            baseMapper.insert(dict);
        }

    }

    /**
     * 收尾方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
